import { AjaxService, Req, Res } from "./ajax.service"

import { Injectable } from '@angular/core'

import { Task } from "./task"
import { Day } from "./day"


@Injectable()
export class TaskService {
  private bh = AjaxService.APIROOT + '/tasks'

  update(task: Task, field?: string): Promise<Res> {
    var argv = { task: { }, token: localStorage.getItem("token") }
    if (field)
      argv.task[field] = task[field]
    else
      argv.task = task
    if (task.id)
      return AjaxService.request(new Req(`${this.bh}/${task.id}.json`, "PUT", argv))
    else
      return AjaxService.request(new Req(`${this.bh}.json`, "POST", argv))
  }


  get(id: string): Promise<Task> {
    var task: Task
    return new Promise<Task>(resolve => {
      AjaxService.request(new Req(`${this.bh}/${id}.json`, "GET"))
          .then(res => {
            if (!res.error)
              task = res.data
            resolve()
          }).catch((err: any) => { console.log("get task error", err) })
    }).then(() => task)
  }

  get_task(args: Object): Promise<Array<Task>> {
    var argv = args
    var tasks: Array<Task> = [ ]
     if (localStorage.getItem("token"))
      argv["token"] = localStorage.getItem("token")
    return new Promise<Task>(resolve => {
      AjaxService.request(new Req(`${this.bh}/task_list.json`, "GET",argv))
          .then(res => {
            if (!res.error)
              tasks = res.data
            resolve()
          }).catch((err: any) => { console.log("get task error", err) })
    }).then(() => tasks)
  }


  getList(args: Object): Promise<Array<Task>> {
    var argv = args
    if (localStorage.getItem("token"))
      argv["token"] = localStorage.getItem("token")
    var tasks: Array<Task> = [ ]
    return new Promise<Array<Task>>(resolve => {
        AjaxService.request(new Req(`${this.bh}.json`, "GET", argv))
          .then(res => {
            if (!res.error)
              tasks = this.tasksInit(res.data)
            resolve()
          }).catch((err: any) => { console.log("get tasks error", err) })
      }).then(() => tasks)
  }


  get_day_hour(args: Object): Promise<Array<Task>> {
    var argv = args
    if (localStorage.getItem("token"))
      argv["token"] = localStorage.getItem("token")
    var tasks: Array<Task> = [ ]
    return new Promise<Array<Task>>(resolve => {
        AjaxService.request(new Req(`${this.bh}/day_hour.json`, "GET", argv))
          .then(res => {
            if (!res.error)
              tasks = this.tasksInit(res.data)
            resolve()
          }).catch((err: any) => { console.log("get tasks error", err) })
      }).then(() => tasks)
  }



  tasksInit(tasks: Array<Task>): Array<Task> {
    var now = new Date()
    var today = new Date(now.getFullYear() + "-" +
                         (now.getMonth() + 1) + "-" + now.getDate())
    return tasks.map(task => {
      task.work_days = 0
      task.used_days = 0
      task.remain_days = 0
      if (task.days) task.days.map(day => {
        day.date = new Date(day.date)
        if (day.work) {
          task.work_days += 1
          if (new Date(day.date) < today)
            task.used_days += 1
          else
            task.remain_days += 1
        }
      })
      return task
    })
  }
}
